// main object
var JC = {
	author:	 "cicciodarkast",
	version: "1.1.1"
};

// manage string base game status
JC.NEWINFO = function(l) {var o="";for(var i=0;i<l;i++)o+=String.fromCharCode(JC.OUTER);return o;}
JC.SETINFO = function(status,offset,info) {return status.substr(0,offset) + String.fromCharCode(info) + status.substr(offset+1);}
JC.GETINFO = function(state,offset) {return state.charCodeAt(offset);}

// advance game constants
JC.START			= "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
JC.BLANK 			= 32; 
JC.OUTER 			= 33;
JC.BLACK 			= 36;
JC.WHITE 			= 37;
JC.FLAT 			= 38;
JC.TURN 			= 144;

JC.BPAWN1 			= 40;
JC.WPAWN1 			= 41;
JC.BPAWN2 			= 42;
JC.WPAWN2 			= 43;
JC.BPAWNC 			= 44;
JC.WPAWNC 			= 45;
JC.BKINGR 			= 46;
JC.WKINGR 			= 47;
JC.BKINGL 			= 48;
JC.WKINGL 			= 49;

JC.WHITEPAWN 		= 80;
JC.WHITEKNIGHT 		= 81;
JC.WHITEBISHOP 		= 82;
JC.WHITEROOK 		= 83;
JC.WHITEQUEEN 		= 84;
JC.WHITEKING 		= 85;

JC.BLACKPAWN 		= 79;
JC.BLACKKNIGHT 		= 78;
JC.BLACKBISHOP 		= 77;
JC.BLACKROOK 		= 76;
JC.BLACKQUEEN 		= 75;
JC.BLACKKING 		= 74;

// aims
JC.BIT = new Array();

// white piece aims constants
JC.BIT[JC.WHITEPAWN] = {};
JC.BIT[JC.WHITEPAWN].len = 4;
JC.BIT[JC.WHITEPAWN].deep = 1;
JC.BIT[JC.WHITEPAWN].delta = new Array(-11,-12,-13,-24);
JC.BIT[JC.WHITEPAWN].control = new Array(JC.WPAWNC,JC.WPAWN1,JC.WPAWNC,JC.WPAWN2);

JC.BIT[JC.WHITEKNIGHT] = {};
JC.BIT[JC.WHITEKNIGHT].len = 8;
JC.BIT[JC.WHITEKNIGHT].deep = 1;
JC.BIT[JC.WHITEKNIGHT].delta = new Array(23,25,-23,-25,14,10,-10,-14);
JC.BIT[JC.WHITEKNIGHT].control = new Array(38,38,38,38,38,38,38,38);

JC.BIT[JC.WHITEBISHOP] = {};
JC.BIT[JC.WHITEBISHOP].len = 4;
JC.BIT[JC.WHITEBISHOP].deep = 7;
JC.BIT[JC.WHITEBISHOP].delta = new Array(-11,-13,11,13);
JC.BIT[JC.WHITEBISHOP].control = new Array(38,38,38,38);

JC.BIT[JC.WHITEROOK] = {};
JC.BIT[JC.WHITEROOK].len = 4;
JC.BIT[JC.WHITEROOK].deep = 7;
JC.BIT[JC.WHITEROOK].delta = new Array(-12,12,-1,1);
JC.BIT[JC.WHITEROOK].control = new Array(38,38,38,38);

JC.BIT[JC.WHITEQUEEN] = {};
JC.BIT[JC.WHITEQUEEN].len = 8;
JC.BIT[JC.WHITEQUEEN].deep = 7;
JC.BIT[JC.WHITEQUEEN].delta = new Array(-11,-13,11,13,-12,12,-1,1);
JC.BIT[JC.WHITEQUEEN].control = new Array(38,38,38,38,38,38,38,38);

JC.BIT[JC.WHITEKING] = {};
JC.BIT[JC.WHITEKING].len = 8;
JC.BIT[JC.WHITEKING].deep = 1;
JC.BIT[JC.WHITEKING].delta = new Array(-11,-13,11,13,-12,12,-1,1);
JC.BIT[JC.WHITEKING].control = new Array(38,38,38,38,38,38,38,38);

// black piece aims constants
JC.BIT[JC.BLACKPAWN] = {};
JC.BIT[JC.BLACKPAWN].len = 4;
JC.BIT[JC.BLACKPAWN].deep = 8;
JC.BIT[JC.BLACKPAWN].delta = new Array(11,12,13,24);
JC.BIT[JC.BLACKPAWN].control = new Array(JC.BPAWNC,JC.BPAWN1,JC.BPAWNC,JC.BPAWN2);

JC.BIT[JC.BLACKKNIGHT] = {};
JC.BIT[JC.BLACKKNIGHT].len = 8;
JC.BIT[JC.BLACKKNIGHT].deep = 1;
JC.BIT[JC.BLACKKNIGHT].delta = new Array(23,25,-23,-25,14,10,-10,-14);
JC.BIT[JC.BLACKKNIGHT].control = new Array(38,38,38,38,38,38,38,38);

JC.BIT[JC.BLACKBISHOP] = {};
JC.BIT[JC.BLACKBISHOP].len = 4;
JC.BIT[JC.BLACKBISHOP].deep = 7;
JC.BIT[JC.BLACKBISHOP].delta = new Array(-11,-13,11,13);
JC.BIT[JC.BLACKBISHOP].control = new Array(38,38,38,38);

JC.BIT[JC.BLACKROOK] = {};
JC.BIT[JC.BLACKROOK].len = 4;
JC.BIT[JC.BLACKROOK].deep = 7;
JC.BIT[JC.BLACKROOK].delta = new Array(-12,12,-1,1);
JC.BIT[JC.BLACKROOK].control = new Array(38,38,38,38);

JC.BIT[JC.BLACKQUEEN] = {};
JC.BIT[JC.BLACKQUEEN].len = 8;
JC.BIT[JC.BLACKQUEEN].deep = 7;
JC.BIT[JC.BLACKQUEEN].delta = new Array(-11,-13,11,13,-12,12,-1,1);
JC.BIT[JC.BLACKQUEEN].control = new Array(38,38,38,38,38,38,38,38);

JC.BIT[JC.BLACKKING] = {};
JC.BIT[JC.BLACKKING].len = 8;
JC.BIT[JC.BLACKKING].deep = 1;
JC.BIT[JC.BLACKKING].delta = new Array(-11,-13,11,13,-12,12,-1,1);
JC.BIT[JC.BLACKKING].control = new Array(38,38,38,38,38,38,38,38);

// status or info manage functions
JC.P 		= function(x,y) {return (y+2)*12+(x+2);}
JC.ISBLANK 	= function(info) {return (i == "");}
JC.COLOR	= function(info) {return info>>4|32;}
JC.ISPIECE	= function(info) {}
JC.ISOUTER	= function(info) {}
JC.EXCEPT	= function() {}

function step0(state) {
	var m = new Array();
	
	var x = 0;
	var y = 0;
	var i = 0;
	var t = 0;
	var s = 0;
	var e = false;
	var h = 0;
	var u = 0;
	var a = {};
	
	for (x=0;x<8;x++) {
		for (y=0;y<8;y++) {
			i = JC.GETINFO(state,JC.P(x,y));
			t = JC.GETINFO(state,JC.TURN);
			if (t == JC.COLOR(i)) {
				a = JC.BIT[i];
				for(s=0;s<a.len;s++) {					
					e = true;
					h = 0;
					d = 0;
					u = 0;
					while(e && (h<a.deep)) {
						d = d + a.delta[s];
						u = JC.GETINFO(state,JC.P(x,y)+d);
						if ((t == JC.COLOR(u))||(JC.OUTER == u)) {
							e = false;
						} else {
							if (a.control[s] != JC.FLAT) {
								if (u == JC.BLANK) {
								
								} else {
									if (JC.WPAWNC == a.control[s]) {
										echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
									}
								}
							} else {
								echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
							}
						}
						h++;
					}	
				}
			}
		}
	}
	return m;
}

/*

								if (false) {
								} else if (a.control[s] == JC.BPAWN1) {
									if (u == JC.BLANK) echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
								} else if (a.control[s] == JC.WPAWN1) {
									if (u == JC.BLANK) echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
								} else if (a.control[s] == JC.BPAWN2) {
									if (y == 1) {
										if (u == JC.BLANK) {
											if (JC.GETINFO(state,JC.P(x,y)+12) == JC.BLANK) {
												echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
											}
										}
									}
								} else if (a.control[s] == JC.WPAWN2) {
									if (y == 6) {
										if (u == JC.BLANK) {
											if (JC.GETINFO(state,JC.P(x,y)-12) == JC.BLANK) {
												echon(coord(JC.P(x,y))+"-"+coord(JC.P(x,y)+d));
											}
										}
									}
								} else if (a.control[s] == JC.BPAWNC) {
									
								} else if (a.control[s] == JC.WPAWNC) {
									
								} else if (a.control[s] == JC.BKINGL) {
									
								} else if (a.control[s] == JC.WKINGL) {
									
								} else if (a.control[s] == JC.BKINGR) {

								} else if (a.control[s] == JC.WKINGR) {
									
								}


*/
// analisys functions
function step1(state) {
	var i = 0;
	var j = 0;
	ms_input = step0(state);
	ms_output = new Array();
	for(i in ms) {
		result = play(ms[i],state);
		if (GETINFO(result,META) != KINGCAPTURED) {
			ms_output[j] = ms[i];
		}
	}
	return ms_output;
}
function step2(status) {
	ms_input = step0(status);
	for(i in ms_input) {
		alfa = play(ms[i],state);
		if (GETINFO(result,META) != KINGCAPTURED) {
			ms_output[j] = ms[i];
		}
	}	
}

// action on status function
function play(m,state) {

}

//  load imput function
JC.fen = function(pos) {
	var status = JC.NEWINFO(200);
	var c = 0;
	var x = 0;
	var y = 0;
	var k = 0;
	for(c=0;c<pos.length;c++) {
		if (false) {
			} else if (pos.charAt(c) == "p") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKPAWN);x++;
			} else if (pos.charAt(c) == "n") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKKNIGHT);x++;
			} else if (pos.charAt(c) == "b") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKBISHOP);x++;
			} else if (pos.charAt(c) == "r") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKROOK);x++;
			} else if (pos.charAt(c) == "q") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKQUEEN);x++;
			} else if (pos.charAt(c) == "k") {status = JC.SETINFO(status,JC.P(x,y),JC.BLACKKING);x++;
			} else if (pos.charAt(c) == "P") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEPAWN);x++;
			} else if (pos.charAt(c) == "N") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEKNIGHT);x++;
			} else if (pos.charAt(c) == "B") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEBISHOP);x++;
			} else if (pos.charAt(c) == "R") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEROOK);x++;
			} else if (pos.charAt(c) == "Q") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEQUEEN);x++;
			} else if (pos.charAt(c) == "K") {status = JC.SETINFO(status,JC.P(x,y),JC.WHITEKING);x++;
			} else if (pos.charAt(c) == "/") {x = 0;y++;
			} else {for(k=0;k<parseInt(pos.charAt(c));k++) {status = JC.SETINFO(status,JC.P(x,y),JC.BLANK);x++;}
		}
	}
	status = JC.SETINFO(status,JC.TURN,JC.WHITE);
	return status;
}





